package com.musiclibrary.scraper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.musiclibrary.common.JsoupUtils;
import com.musiclibrary.persistence.MusicAlbums;
import com.musiclibrary.persistence.MusicCategory;

public class MusicCategoryParser {

	public MusicCategory parseMusicCategory(Element element,final WebPage<Map<String, String>> webPage) {
		MusicCategory musicCategory = null;
		try {
			if (element != null) {
				musicCategory=new MusicCategory();

				Elements elements= element.select("a");
				Element hrefElement = elements.first();
				// retrieve  Name from element
				String dirName = hrefElement.html();
				musicCategory.setCategoryName(dirName);
				
				// retrieve URL from element
				String url = hrefElement.attr("href");
				musicCategory.setCategoryUrl(url);

				//update webPage with request parameter
				webPage.setUrl(url);				
				webPage.getFormHandler().updateForm(null);
				//fetch site data 
				JsoupUtils jsoupUtils = new JsoupUtils();
				Document document = jsoupUtils.loadPage(webPage);
				
				//retrieve music Albums 
				List<Element> musicAlbumsElementList=new ArrayList<Element>();
				if(document!=null){
					//update web page URL to fetch music albums from site
					webPage.setUrl("http://telugump3downloads.net/music/index.php");
					
					//read header
					Elements headerElements = document.select("body div.head2 b");
					Element header = headerElements.first();
					String title = header.text();
					//header end
					
					//read category element from current page
					Elements elementList = document.select("body div.bg");
					if(elementList!=null && elementList.size()>0){
						musicAlbumsElementList.addAll(elementList);
					}
					//read album element end
					
					//read album element from other pages

					//read index count
					Element bodyElement = document.tagName("body");
					String html = bodyElement.html();
					String pageStart="0";
					String pageEnd="0";
					if (html.contains("</div>Page:")) {
						String substring = html.substring(html.indexOf("</div>Page:"), html.indexOf("<br />"));
						pageStart = substring.substring(substring.indexOf(":") + 1, substring.indexOf("O")).trim();
						pageEnd = substring.substring(substring.lastIndexOf("Of") + 2, substring.length()).trim();
					}
					
					int startCount = Integer.parseInt(pageStart);
					int endCount = Integer.parseInt(pageEnd);
					//read index count end

					for(int i=startCount;i<endCount;i++){
						//update web page MAP to fetch music albums from site
						HashMap<String, String> hashMap = new HashMap<String, String>();
						hashMap.put("dir", "/"+title);
						hashMap.put("p", "1");
						hashMap.put("page", Integer.toString(i));
						hashMap.put("sort", "0");
						webPage.getFormHandler().updateForm(hashMap);
						
						//fetch from site
						Document _document = jsoupUtils.loadPage(webPage);
						if(_document!=null){
							Elements _elementList = _document.select("body div.bg");
							if(_elementList!=null && _elementList.size()>0){
								musicAlbumsElementList.addAll(_elementList);
							}														
						}						
					}
					//read other pages end
					
					//construct music Albums list from element

					MusicAlbumParser musicAlbumParser = new MusicAlbumParser();
					if(musicAlbumsElementList.size()>0){
						List<MusicAlbums> musicAlbums=new ArrayList<MusicAlbums>();
						for(Element albumElement:musicAlbumsElementList){
							MusicAlbums musicAlbum = musicAlbumParser.parseMusicAlbums(albumElement, webPage, title);
							musicAlbum.setMusicCategory(musicCategory);
							musicAlbums.add(musicAlbum);
						}
						musicCategory.setMusicAlbums(musicAlbums);
					}
					//construct music albums list end
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return musicCategory;
	}
}
